package com.zork.cmdb.webapi.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zork.cmdb.webapi.common.exception.CmdbException;
import com.zork.cmdb.webapi.common.exception.CmdbModuleNotFoundException;
import com.zork.cmdb.webapi.domain.dto.ModuleDTO;
import com.zork.cmdb.webapi.domain.vo.ModuleAttributesVO;
import com.zork.cmdb.webapi.mapper.cmdb.ModuleInstanceMapper;
import com.zork.cmdb.webapi.service.ModuleAttributesService;
import com.zork.cmdb.webapi.service.ModuleInstanceService;
import com.zork.cmdb.webapi.service.ModuleService;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author chenjh
 * @date 2018/5/16
 * @description
 */
@Service
public class ModuleInstanceServiceImpl implements ModuleInstanceService {

    @Autowired
    private ModuleInstanceMapper moduleInstanceMapper;

//    @Autowired
//    private JdbcTemplate jdbcTemplate;

    @Autowired
    private ModuleService moduleService;

    @Autowired
    private ModuleAttributesService moduleAttributesService;

    @Override
    public PageInfo<Map<String, Object>> listInstance(Integer moduleID, Integer instanceID, Integer pageNum, Integer pageSize, String... wheresql) throws CmdbException {
        StringBuffer sql = getListInstanceSql(moduleID);
        if (instanceID != null) {
            sql.append(" AND [ID] = " + instanceID);
        }
        if (wheresql != null && wheresql.length > 0) {
            for (int i = 0; i < wheresql.length; i++) {
                sql.append(wheresql[i]);
            }
        }
        sql.append(" ORDER BY [ID]");
        PageInfo<Map<String, Object>> result = PageHelper.startPage(pageNum, pageSize).doSelectPageInfo(() -> moduleInstanceMapper.listInstance(sql.toString()));
        return result;
    }

    @Override
    public StringBuffer getListInstanceSql(Integer moduleID) throws CmdbException {
        ModuleDTO module = moduleService.getModule(moduleID);
        if (module == null) {
            throw new CmdbModuleNotFoundException();
        }
        List<ModuleAttributesVO> attributes = moduleAttributesService.listAttributes(moduleID);
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT");
        sql.append(" m.[ID] AS [moduleInstanceID]");
        sql.append(", wi.[ModuleID] AS [moduleID]");
        sql.append(", wi.[CICode] AS [ciCode]");
        sql.append(", wi.[MaintainGroupID] AS [maintainGroupID]");
        sql.append(", wi.[SourceInstanceID] AS [remedyID]");
        for (ModuleAttributesVO attribute : attributes) {
            if (attribute.getIssable()) {
                if ("REFERENCE".equals(attribute.getAttributesType())) {
                    String referenceInstanceTablename = "";
//                    String sql1 = "SELECT [Value] as [value] FROM  [ModuleAttributesOption] where [AttributesID] = ?";
//                    Object refModuleIDObj = jdbcTemplate.queryForMap(sql1, attribute.getAttributesID()).get("value");
//                    Map<String, Object> refModuleIDMap = moduleInstanceMapper.getRefModuleID(attribute.getAttributesID());
//                    if (refModuleIDMap == null && refModuleIDMap.size() > 0) {
                        Map<String, Object> refModuleIDMap = moduleInstanceMapper.getRefModuleID(attribute.getAttributesID());
                        if (refModuleIDMap != null && refModuleIDMap.get("value") != null) {
                            int refModuleID = Integer.parseInt(refModuleIDMap.get("value").toString());
                            ModuleDTO refModule = moduleService.getModule(refModuleID);
                            if (refModule != null) {
                                referenceInstanceTablename = refModule.getInstanceTableName();
                            }
                        }
//                    }
                    if ("".equals(referenceInstanceTablename)) {
                        sql.append(String.format(", NULL AS [%s]", attribute.getAttributesName()));
                    } else {
                        sql.append(String.format(", (SELECT [Name] FROM [%s] WHERE ID = m.[%s]) AS [%s]", referenceInstanceTablename, attribute.getInstanceColumnName(), attribute.getAttributesName()));
                    }

                } else if ("BOOLEAN".equals(attribute.getAttributesType())) {
                    sql.append(String.format(", (CASE m.[%s] WHEN 1 THEN '是' WHEN 0 THEN '否' ELSE NULL END) AS [%s]", attribute.getInstanceColumnName(), attribute.getAttributesName()));
                } else if ("DATE".equals(attribute.getAttributesType())) {
                    sql.append(String.format(", SUBSTRING(m.[%s], 0, 11) As [%s]", attribute.getInstanceColumnName(), attribute.getAttributesName()));
                } else {
                    sql.append(String.format(", m.[%s] As [%s]", attribute.getInstanceColumnName(), attribute.getAttributesName()));
                }
            }
        }
        sql.append(" FROM " + String.format("[%s]", module.getInstanceTableName()) + " m, [WarehouseInstance] wi");
        sql.append(" WHERE wi.[ModuleID] = m.[ModuleID] AND wi.[ModuleInstanceID] = m.[ID] AND wi.[IsDeleted] = 0");
        return sql;
    }
}
